﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.ServiceModel;
using System.Web;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.InterceptionExtension;
using WmsService.BusinessLogic;
using WmsService.Security;

namespace WmsService.AOP
{
    public class InvoiceBillHistoryCallHandler : ICallHandler
    {
        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            //开始获取实体
            InvoiceBill invoiceBill = null;

            if(input.Arguments.Count != 1)
            {
                throw new AopException("[InvoiceBillHistoryCallHandler] 特性绑定方法错误：" + input.MethodBase.Name);
            }

            var logic = (IInvoiceAndPayApplyBillLogic)input.Target;
            invoiceBill = logic.GetInvoiceBill((String)input.Arguments[0]);
            if(null == invoiceBill )
            {
                throw new AopException("[InvoiceBillHistoryCallHandler] 特性绑定方法错误：发票不存在：" + (String)input.Arguments[0] +"，"+ input.MethodBase.Name);
            }

            //执行方法，如果没有异常，就执行下面的保存历史记录方法
            InvokeHandlerDelegate invokeHandlerDelegate = getNext();
            var methodReturn = invokeHandlerDelegate(input, getNext);

            var invoiceBillHistory = InvoiceBillHistory.CreateInvoiceBillHistory(0, invoiceBill.InvoiceNo,
                                                                                 invoiceBill.InvoiceTag,
                                                                                 invoiceBill.ProviderCode,
                                                                                 invoiceBill.AmountTax,
                                                                                 invoiceBill.InStoreBillAmount);

            var propertyInfos = invoiceBill.GetType().GetProperties();

            PropertyInfo pi;
            foreach (var propertyInfo in propertyInfos)
            {
                pi = typeof(InvoiceBillHistory).GetProperty(propertyInfo.Name);
                if(pi != null && pi.Name != "EntityKey")
                {
                    try
                    {
                        pi.SetValue(invoiceBillHistory, propertyInfo.GetValue(invoiceBill, null), null);
                    }
                    catch{}
                }
            }

            invoiceBillHistory.OperationTime = DateTime.Now;
            invoiceBillHistory.ExtVarChar3 = "删除该记录";
            var logicBase = (LogicBase)input.Target;
            invoiceBillHistory.ExtVarChar4 = logicBase.ContextUserID;

            var ctx = Factory.ContextFactory.Create();
            ctx.AddToInvoiceBillHistory(invoiceBillHistory);
            ctx.SaveChanges();

            return methodReturn;
        }
        public int Order { get; set; }
    }

    public class InvoiceBillHistoryCallHandlerAttribute : HandlerAttribute
    {
        public override ICallHandler CreateHandler(IUnityContainer container)
        {
            return new InvoiceBillHistoryCallHandler { Order = this.Order };
        }
    }
}